package ling.learn.syntax;

import java.util.ArrayList;
import java.util.List;

public class SyntaxEdge {
  String from;
  String to;
  List<String> contents;
  boolean hasLoopOrSelect;
  boolean loop;


  public SyntaxEdge(String from, String to) {
    this.from = from;
    this.to = to;
    this.contents = new ArrayList<String>();
    this.hasLoopOrSelect = false;
    if (from.equals(to)) {
      this.loop = true;
    } else {
      this.loop = false;
    }
  }

  public void addContent(String content) {
    if (content.isEmpty()) {
      this.hasLoopOrSelect = true;
    } else {
      this.contents.add(content);
    }
  }

  public String toString() {
    String loopOrSelect = "";
    if (this.hasLoopOrSelect) {
      loopOrSelect = "?";
    }
    if (this.loop) {
      loopOrSelect = "*";
    }
    if (contents.size() > 1) {
      return String.format("( %s )%s",
          new Object[] {String.join(" | ", this.contents), loopOrSelect});
    } else {
      return String.format("%s%s", new Object[] {String.join(" | ", this.contents), loopOrSelect});
    }
  }
}
